package com.njy.gateway.filter;


import cn.hutool.core.lang.UUID;
import com.njy.pojo.Constant;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.result.view.RequestContext;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.format.SignStyle;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
    private final static Logger LOGGER = LoggerFactory.getLogger(MyGlobalFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取request和response对象
        ServerHttpRequest request = exchange.getRequest();
        //ServerHttpResponse response = exchange.getResponse();

     /*   String name = exchange.getRequest().getQueryParams().getFirst("name");
        if(name == null){
            log.info("---非法用户---");
            exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
            return exchange.getResponse().setComplete();
        }*/
        String TRACE_ID = UUID.randomUUID().toString();
        //LOGGER.info("MyGlobalFilter--->"+TRACE_ID);
        ServerHttpRequest.Builder mutate = request.mutate();
        //将TRACE_ID发送到下游微服务
        mutate.header(Constant.TRACE_ID,TRACE_ID);
        mutate.method(HttpMethod.POST);

        //将TRACE_ID发送到下游微服务
        //LinkedHashMap<String, String> headerMap = new LinkedHashMap<>();
        //headerMap.put(Constant.TRACE_ID, TRACE_ID);

        //chainFilterAndSetHeaders(chain, exchange, headerMap);


        return chain.filter(exchange.mutate().request(mutate.build()).build());
    }

    @Override
    public int getOrder() {
        return 0;
    }

    /**
     * 设置向后传递的header
     *
     * @param chain
     * @param exchange
     * @param headerMap
     */
    private Mono<Void> chainFilterAndSetHeaders(GatewayFilterChain chain, ServerWebExchange exchange, LinkedHashMap<String, String> headerMap) {
        // 添加header
        Consumer<HttpHeaders> httpHeaders = httpHeader -> {
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                // 遍历Map设置header，向后传递
                httpHeader.set(entry.getKey(), entry.getValue());
            }
        };

        ServerHttpRequest newRequest = exchange.getRequest().mutate().headers(httpHeaders).build();
        ServerWebExchange build = exchange.mutate().request(newRequest).build();
        //将现在的request 变成 exchange对象
        return chain.filter(build);
    }

}
